package spring.angularjs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.angularjs.domain.Role;
import spring.angularjs.domain.Status;
import spring.angularjs.domain.Userinfo;
import spring.angularjs.domain.json.UserTemp;
import spring.angularjs.services.Services;

@Controller
@RequestMapping(value = "/create")
public class CreateController {

	@Autowired
	private Services services;

	private byte[] imageTemp = null;
	
 	@RequestMapping(value = "/start")
	public String goPartialCreate() {
		return "detail/create";
	}

	@RequestMapping(value = "/file/upload", method = RequestMethod.POST)
	@ResponseBody
	public void fileUpload() {
	}
	
	@RequestMapping(value = "/file/image", method = RequestMethod.POST)
	@ResponseBody
	public byte[] createImageUpload(@RequestBody byte[] data) {
		imageTemp = data;
		return data;
	}

	@RequestMapping(value = "/user", method = RequestMethod.POST)
	@ResponseBody
	public void createUser(@RequestBody UserTemp userTemp) {
		HomeController.page = "create";
		Role role = services.getRole(userTemp.getRoleid());
		Status status = services.getStatus(userTemp.getStatusid());
		BCryptPasswordEncoder encode = new BCryptPasswordEncoder();
		String newpassword = encode.encode(userTemp.getPassword());
		Userinfo user = new Userinfo(userTemp.getLoginId(), status, role,
				newpassword, userTemp.getCardnumberid(),
				userTemp.getFirstname(), userTemp.getLastname(),
				userTemp.getMidname(), userTemp.getPhone1(),
				userTemp.getPhone2(), userTemp.getAddress1(),
				userTemp.getAddress2(), userTemp.getEmail1(),
				userTemp.getEmail2(), imageTemp);
		services.save(user);
	}
	
	@RequestMapping(value = "/checkLoginId", method = RequestMethod.POST)
	@ResponseBody
	public boolean checkLoginId(@RequestParam String loginId) {
		Userinfo user = services.getUserInfo(loginId);
		if (user == null) {
			return true;
		} else {
			return false;
		}
	}
	
	@RequestMapping(value="/checkcardnumber", method = RequestMethod.POST)
	@ResponseBody
	public boolean checkCardNumberId(@RequestParam String cardnumberid){
		if(cardnumberid.matches("\\d{9}")){
			return true;
		} else {
			return false;
		}
	}
	
	@RequestMapping(value = "/checkfirstname", method=RequestMethod.POST)
	@ResponseBody
	public boolean checkFistName(@RequestParam String firstname){
		if(firstname.matches("\\D+")){
			return true;
		} else {
			return false;
		}
	}
	
	@RequestMapping(value = "/checklastname", method=RequestMethod.POST)
	@ResponseBody
	public boolean checkLastName(@RequestParam String lastname){
		if(lastname.matches("\\D+")){
			return true;
		} else {
			return false;
		}
	}
	
	@RequestMapping(value = "/checkmidname", method=RequestMethod.POST)
	@ResponseBody
	public boolean checkMidName(@RequestParam String midname){
		if(midname.matches("\\D+")){
			return true;
		} else {
			return false;
		}
	}
	
	@RequestMapping(value="/checkphonefirst", method=RequestMethod.POST)
	@ResponseBody
	public boolean checkPhoneFirst(@RequestParam String phone1){
		if(phone1.matches("\\d{10}")){
			return true;
		} else if(phone1.matches("\\d{11}")){
			return true;
		} else {
			return false;
		}
	}
	
	@RequestMapping(value="/checkphonesecond", method=RequestMethod.POST)
	@ResponseBody
	public boolean checkPhoneSecond(@RequestParam String phone2){
		if(phone2.matches("\\d{10}")){
			return true;
		} else if(phone2.matches("\\d{11}")){
			return true;
		} else {
			return false;
		}
	}
}
